//
//  KVNLocationManager.m
//  Test1
//
//  Created by KelvinYeung on 15/8/1.
//  Copyright (c) 2015年 KelvinYeung. All rights reserved.
//

#import "KVNLocationManager.h"

@interface KVNLocationManager() <CLLocationManagerDelegate>
@property CLLocationManager *locationManager;
@property CLLocation * location;
@property CLGeocoder *geocoder;
@end

@implementation KVNLocationManager
@synthesize locationManager;
@synthesize location;
@synthesize geocoder;

- (void)startStandardUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
    
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    
    // Set a movement threshold for new events.
    locationManager.distanceFilter = 500; // meters
    
    [locationManager startUpdatingLocation];
}


- (void)startSignificantChangeUpdates
{
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
    
    locationManager.delegate = self;
    [locationManager startMonitoringSignificantLocationChanges];
}



// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)aLocations {
    // If it's a relatively recent event, turn off updates to save power.
    CLLocation* aLocation = [aLocations lastObject];
    NSDate* eventDate = location.timestamp;
    NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
    if (fabs(howRecent) < 15.0) {
        // If the event is recent, do something with it.
        NSLog(@"latitude %+.6f, longitude %+.6f\n",
              aLocation.coordinate.latitude,
              aLocation.coordinate.longitude);
    }
}


- (NSString*) getCurrentCityName{
    if(!geocoder)
        geocoder = [[CLGeocoder alloc] init];
    
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        
        

        if ([placemarks count] > 0)
        {
            //annotation.placemark = [placemarks objectAtIndex:0];

            // Add a More Info button to the annotation's view.
          
        }
        
        
        
        
    }];


    return nil;
}





@end
